﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace QuickModel.InteractiveOperation.CmdTestInteractiveOperation
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdTestGetText : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = new Transaction(commandData.Application.ActiveUIDocument.Document);
			Result result;
			try
			{
				DwgEntitiesSelection dwgEntitiesSelection = new DwgEntitiesSelection(commandData.Application.ActiveUIDocument);
				List<TextInfo> lstAllEntity = new List<TextInfo>();
				bool flag = !dwgEntitiesSelection.TryPickSameLayerText(out lstAllEntity, false, "请选择文字");
				if (flag)
				{
					bool flag2 = dwgEntitiesSelection.Result == DwgEntitiesSelectionResult.eUnitError;
					if (flag2)
					{
						YJKMessageBox.Information("链接图纸的单位不是“毫米”，请重新链接图纸并将导入单位设置为“毫米”");
					}
					else
					{
						bool flag3 = dwgEntitiesSelection.Result == DwgEntitiesSelectionResult.eDwgPathError;
						if (flag3)
						{
							YJKMessageBox.Information("所链接图纸的源文件不存在，请重新链接。");
						}
					}
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					List<TextInfo> list = new List<TextInfo>();
					bool flag4 = !dwgEntitiesSelection.TryGetEntitesInPickBox<TextInfo>(out list, lstAllEntity, delegate(TextInfo txt, PickedBox pickBox)
					{
						XYZ hyendPoint = txt.GetBaseLine().GetEndPoint(0);
						XYZ xyz = new XYZ(Math.Min(pickBox.Min.X, pickBox.Max.X), Math.Min(pickBox.Min.Y, pickBox.Max.Y), 0.0);
						XYZ xyz2 = new XYZ(Math.Max(pickBox.Min.X, pickBox.Max.X), Math.Max(pickBox.Min.Y, pickBox.Max.Y), 0.0);
						return hyendPoint.X > xyz.X && hyendPoint.Y > xyz.Y && hyendPoint.X < xyz2.X && hyendPoint.Y < xyz2.Y;
					}, "请框选范围");
					if (flag4)
					{
						bool flag5 = dwgEntitiesSelection.Result == DwgEntitiesSelectionResult.eUnitError;
						if (flag5)
						{
							YJKMessageBox.Information("链接图纸的单位不是“毫米”，请重新链接图纸并将导入单位设置为“毫米”");
						}
						else
						{
							bool flag6 = dwgEntitiesSelection.Result == DwgEntitiesSelectionResult.eDwgPathError;
							if (flag6)
							{
								YJKMessageBox.Information("所链接图纸的源文件不存在，请重新链接。");
							}
						}
						result = Autodesk.Revit.UI.Result.Cancelled;
					}
					else
					{
						transaction.Start("创建文字");
						foreach (TextInfo textInfo in list)
						{
							bool flag7 = textInfo.UseText.IsEmpty<char>();
							if (!flag7)
							{
								foreach (Curve curCurve in textInfo.BoundingBoxCurve)
								{
									TestCommon.CreateModelCurve(curCurve, commandData.Application.ActiveUIDocument.Document, new Autodesk.Revit.DB.Color(0, byte.MaxValue, 0), 1);
								}
							}
						}
						transaction.Commit();
						YJKMessageBox.Information("执行成功。");
						result = Autodesk.Revit.UI.Result.Succeeded;
					}
				}
			}
			catch (Exception)
			{
				transaction.RollBack();
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}
	}
}
